//
//  CashSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "CashSettingViewController.h"


@implementation CashSettingViewController
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"현금 관리";        
    [self.tableView setAllowsSelectionDuringEditing:YES];
    
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllCash];    
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    CashFormViewController* viewController = [[[CashFormViewController alloc] initWithNibName:@"CashFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode];
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CashFormViewController* viewController = [[[CashFormViewController alloc] initWithNibName:@"CashFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController loadCash:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"cash_id"]];        
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SQLHandler *sql = [SQLHandler sharedInstance];
    if([_dataSourceArray count]==1){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"경고" message:@"최소 한개는 있어야 합니다." delegate:nil cancelButtonTitle:@"확인" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [self showDoneMode];
    }else{
        [sql deleteCashById:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"cash_id"]];
        if([[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"is_default"] isEqualToString:@"y"]){
            NSDictionary *firstDic = [[sql findAllCash] objectAtIndex:0];
            [sql updateCashId:[firstDic objectForKey:@"cash_id"] 
                        title:[firstDic objectForKey:@"title"]  
                    isDefault:YES];
        }
    }
    self.dataSourceArray = [sql findAllCash];
    [self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath
                 toIndexPath:toIndexPath 
                   tableName:@"cash"];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil){
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.highlightedTextColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.showsReorderControl = YES;
	}
    if([[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"is_default"] isEqualToString:@"y"]){
        cell.textLabel.text = [[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"title"] stringByAppendingString:@" (기본)"];
    }else{
        cell.textLabel.text = [[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"title"];
    }
	
	return cell;
}
@end
